<?php
namespace haibao\app\model;

class Mail{
	const PRIORITY_TYPE_LOW = 1;
	const PRIORITY_TYPE_MIDDLE = 2;
	const PRIORITY_TYPE_HIGH = 3;
	
    public $From;
    public $To;
    public $Name;
    public $Reply;
    public $Subject;
    public $Content;
    public $Bcc;
    public $Callback;
    public $Priority;
    public $Sender = \haibao\api\Config::MAIL_SENDER_INFO;

    private $files;
    private $parameters;
    
    public function __construct($to,$subject,$content,$reply=null,$name=null,$bcc=null,$sender=null,$priority=self::PRIORITY_TYPE_HIGH){
        $this->parameters = array();
        $this->To = array();      
        $this->Subject = $subject;
        $this->Content = $content;
        $this->Name = $name;
        $this->Reply = $reply;
        $this->Bcc = $bcc;
        $this->Priority = $priority;
        $this->files = array();
        if ($sender){
        	$this->Sender = $sender;
        	$this->From = $sender;
        }
        else{
        	$this->Sender = \haibao\api\Config::MAIL_SENDER_INFO;
        	$this->From = \haibao\api\Config::MAIL_SENDER_INFO;
        }
       
        
        if (is_string($to))
        {
	        $this->To = array($to);
        }
        else if (is_array($to))
        {
            $this->To = $to;
        }
    }
    
    public function setSender($sender){
    	$this->Sender = $sender;
    }
    
    public function setReply($reply){
        $this->Reply = $reply;
    }
    
    public function setName($name){
        $this->Name = $name;
    }
    
    public function appendTo($mailTo){
        array_push($this->To,$mailTo);
    }
    
    public function setParameter($name,$value,$mailTo=null){
    	if (!$mailTo){
    	    $this->setMailParameter($this->To[0],$name,$value);
    	}
    	else{
	        $this->setMailParameter($mailTo,$name,$value);
    	}
    }
    
    public function setParameters($parameters){
    	$this->parameters = $parameters;
    }
    
    public function getParameters(){
        return $this->parameters;
    }
    
    public function addAttachment($fileName,$fileContent){
        $this->files[$fileName] = $fileContent;
    }
    
    public function getAttachments(){
        return $this->files;
    }
    
    public function changeFrom($from){
        $this->From = $from;
    }
    
    public function setCallback($url){
    	$this->Callback = $url;
    }
    
    private function setMailParameter($mail,$name,$value){
        if (!isset($this->parameters[$mail])){
            $this->parameters[$mail] = array();
        }
        $this->parameters[$mail][$name] = $value;
    }
}
?>
